package com.zhudeshuai.zhudemolist;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zhudeshuai.zhudemolist.activity.CardViewDemoActivity;
import com.zhudeshuai.zhudemolist.activity.CommonDialogDemoActivity;
import com.zhudeshuai.zhudemolist.activity.CustomerViewActivity;
import com.zhudeshuai.zhudemolist.activity.ImageScaleHandleActivity;
import com.zhudeshuai.zhudemolist.activity.LayerListDemoActivity;
import com.zhudeshuai.zhudemolist.activity.LoadingProgressActivity;
import com.zhudeshuai.zhudemolist.activity.MaterialDesignActivity;
import com.zhudeshuai.zhudemolist.activity.MeasureDemoActivity;
import com.zhudeshuai.zhudemolist.activity.MyScrollViewActivity;
import com.zhudeshuai.zhudemolist.activity.OutGoingActivity;
import com.zhudeshuai.zhudemolist.activity.PhotoDealActivity;
import com.zhudeshuai.zhudemolist.activity.PlayerDemoActivity;
import com.zhudeshuai.zhudemolist.activity.RealmDemoActivity;
import com.zhudeshuai.zhudemolist.activity.RetrofitDemoActivity;
import com.zhudeshuai.zhudemolist.activity.SurfaceViewDemoActivity;
import com.zhudeshuai.zhudemolist.activity.TabLayoutDemoActivity;
import com.zhudeshuai.zhudemolist.activity.TimeLineDemoActivity;
import com.zhudeshuai.zhudemolist.activity.ToolBarDemoActivity;
import com.zhudeshuai.zhudemolist.activity.TouchDemoActivity;
import com.zhudeshuai.zhudemolist.activity.TwoListActivity;
import com.zhudeshuai.zhudemolist.activity.ViewDragHelperActivity;
import com.zhudeshuai.zhudemolist.activity.WebViewTestActivity;
import com.zhudeshuai.zhudemolist.animation.AnimationActivity;
import com.zhudeshuai.zhudemolist.base.BaseActivity;
import com.zhudeshuai.zhudemolist.base.DividerItemDecoration;
import com.zhudeshuai.zhudemolist.bean.DemoItemBean;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

	private RecyclerView mRecyclerView;
	//数据
	private List<DemoItemBean> mDataList = new ArrayList<>();

	private boolean flag;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
		setSupportActionBar(toolbar);

		FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
		fab.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				if (flag) {
					mRecyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
				} else {
					mRecyclerView.setLayoutManager(new GridLayoutManager(view.getContext(), 3));
				}
				flag = !flag;
				/*Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
						.setAction("Action", null).show();*/
			}
		});


		mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
		initData();
		doInitRecyclerView();
	}

	private void initData() {
		mDataList.add(new DemoItemBean("CardView Demo", CardViewDemoActivity.class));
		mDataList.add(new DemoItemBean("ViewDragHelper Demo", ViewDragHelperActivity.class));
		mDataList.add(new DemoItemBean("retrofit入门", RetrofitDemoActivity.class));
		mDataList.add(new DemoItemBean("ToolBar入门", ToolBarDemoActivity.class));
		mDataList.add(new DemoItemBean("SurfaceView Demo", SurfaceViewDemoActivity.class));
		mDataList.add(new DemoItemBean("Realm 数据持久化方案Demo", RealmDemoActivity.class));
		mDataList.add(new DemoItemBean("图片处理Demo", PhotoDealActivity.class));
		mDataList.add(new DemoItemBean("自定义视频播放器", PlayerDemoActivity.class));
		mDataList.add(new DemoItemBean("Material Design主题", MaterialDesignActivity.class));
		mDataList.add(new DemoItemBean("WebView测试不显示问题", WebViewTestActivity.class));
		mDataList.add(new DemoItemBean("touch事件传递问题", TouchDemoActivity.class));
		mDataList.add(new DemoItemBean("TabLayout使用", TabLayoutDemoActivity.class));
		mDataList.add(new DemoItemBean("时间抽view", TimeLineDemoActivity.class));
		mDataList.add(new DemoItemBean("自定义view中OnMeasure方法的使用", MeasureDemoActivity.class));
		mDataList.add(new DemoItemBean("CommonDialog优化", CommonDialogDemoActivity.class));
		mDataList.add(new DemoItemBean("去电状态监听", OutGoingActivity.class));
		mDataList.add(new DemoItemBean("自定义view", CustomerViewActivity.class));
		mDataList.add(new DemoItemBean("双列List", TwoListActivity.class));
		mDataList.add(new DemoItemBean("属性动画学习", AnimationActivity.class));
		mDataList.add(new DemoItemBean("图片压缩优化", ImageScaleHandleActivity.class));
		mDataList.add(new DemoItemBean("自定义滚动效果", MyScrollViewActivity.class));
		mDataList.add(new DemoItemBean("LoadingProgressDialog优化", LoadingProgressActivity.class));
		mDataList.add(new DemoItemBean("lay-list学习demo", LayerListDemoActivity.class));

	}

	/**
	 * recyclerView初始化设置
	 */
	private void doInitRecyclerView() {

		mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
		mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
		mRecyclerView.setHasFixedSize(true);
		mRecyclerView.setItemAnimator(new MyItemAnimator());
		RecyclerViewAdapter adapter = new RecyclerViewAdapter(mDataList);
		mRecyclerView.setAdapter(adapter);
		adapter.setOnItemClickListener(new ItemClickListener() {
			@Override
			public void OnItemClick(View view, int position) {
				DemoItemBean demoItemBean = mDataList.get(position);
				open(MainActivity.this, demoItemBean.getClazz());
			}
		});

	}


	/**
	 * ViewHolder
	 */
	private class RecyclerViewHolder extends RecyclerView.ViewHolder {

		public TextView contentView;

		public RecyclerViewHolder(View itemView, final ItemClickListener listener) {
			super(itemView);
			contentView = (TextView) itemView.findViewById(R.id.content_tv);
			if (listener != null) {
				itemView.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						listener.OnItemClick(v, getPosition());
					}
				});
			}
		}

	}

	/**
	 * Adapter
	 */
	public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {
		private ItemClickListener mItemClickListener;
		private final List<DemoItemBean> mDataList;

		private RecyclerViewAdapter(List<DemoItemBean> mDataList) {
			this.mDataList = mDataList;
		}

		public void setOnItemClickListener(ItemClickListener listener) {
			this.mItemClickListener = listener;
		}

		@Override
		public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
			View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_recycler_item, parent, false);
			return new RecyclerViewHolder(inflate, mItemClickListener);
		}

		@Override
		public void onBindViewHolder(RecyclerViewHolder holder, int position) {
			holder.contentView.setText(mDataList.get(position).getTitle());
		}

		@Override
		public int getItemCount() {
			return mDataList.size();
		}

	}

	public interface ItemClickListener {
		void OnItemClick(View view, int position);
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();

		//noinspection SimplifiableIfStatement
		if (id == R.id.action_settings) {
			return true;
		}

		return super.onOptionsItemSelected(item);
	}
}
